package com.bysj.studentsystem.controller;

import com.bysj.studentsystem.vo.ResponseDataVO;
import com.bysj.studentsystem.entity.SysUser;
import com.bysj.studentsystem.service.SysUserService;
import com.bysj.studentsystem.util.RedisCacheUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * (SysUser)表控制层
 *
 * @author makejava
 * @since 2024-03-09 16:51:51
 */
@CrossOrigin
@RestController
@RequestMapping("user")
public class SysUserController {

    /**
     * 服务对象
     */
    @Resource
    private SysUserService sysUserService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private RedisCacheUtil redisCacheUtil;


    /**
     * 登录
     * @param user
     * @return
     */
    @PostMapping("/login")
    public ResponseDataVO login(@RequestBody SysUser user){
        // 登录
        return new ResponseDataVO().ok(sysUserService.login(user));
    }

    /**
     * 登出
     * @return
     */
    @GetMapping("/logout")
    public ResponseDataVO logout(){
        if (sysUserService.logout()){
            return new ResponseDataVO().ok("注销成功");
        }
        return new ResponseDataVO().fail("注销失败");
    }

    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @return 查询结果
     */
    @GetMapping("/page")
    public ResponseDataVO queryByPage(@RequestParam(value = "username", required = false) String username,
                                      @RequestParam(value = "nickname", required = false) String nickname,
                                      @RequestParam(value = "classInfo", required = false) String classInfo,
                                      @RequestParam(value = "pageNum", required = false) Integer pageNum,
                                      @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        if (pageNum == null || pageSize == null) {
            pageNum = 1;
            pageSize = 10;
        }
        SysUser sysUser = new SysUser();
        sysUser.setUsername(username);
        sysUser.setNickname(nickname);
        sysUser.setClassInfo(classInfo);
        return new ResponseDataVO().ok(this.sysUserService.queryByPage(sysUser, pageNum, pageSize));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseDataVO<SysUser> queryById(@PathVariable("id") Integer id) {
        return new ResponseDataVO().ok(sysUserService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param sysUser 实体
     * @return 新增结果
     */
    @PostMapping
    @PreAuthorize("hasAuthority('admin')")
    public ResponseDataVO add(@RequestBody SysUser sysUser) {
        // 用户密码加密
        sysUser.setPassword(passwordEncoder.encode(sysUser.getPassword()));
        return new ResponseDataVO().ok(this.sysUserService.insert(sysUser));
    }

    /**
     * 编辑数据
     *
     * @param sysUser 实体
     * @return 编辑结果
     */
    @PutMapping
    @PreAuthorize("hasAuthority('admin')")
    public ResponseDataVO edit(@RequestBody SysUser sysUser) {
        return new ResponseDataVO().ok(this.sysUserService.update(sysUser));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    @PreAuthorize("hasAuthority('admin')")
    public ResponseDataVO deleteById(@RequestParam("id") Integer id) {
        if (this.sysUserService.deleteById(id)){
            return new ResponseDataVO().ok();
        }
        return new ResponseDataVO().fail("用户信息不存在");
    }

}

